﻿using DeviceDebugger.View;
using GalaSoft.MvvmLight.Command;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace DeviceDebugger.CustomControl
{
    /// <summary>
    /// WindowTitle.xaml 的交互逻辑
    /// </summary>
    public partial class WindowTitle : UserControl
    {
        #region 属性构造器
        /// <summary>
        /// 标题栏背景颜色
        /// </summary>
        public Brush TitleBackground
        {
            get { return (Brush)this.GetValue(TitleBackgroundProperty); }
            set { this.SetValue(TitleBackgroundProperty, value); }
        }
        public static readonly DependencyProperty TitleBackgroundProperty =
            DependencyProperty.Register("TitleBackground", typeof(Brush), typeof(WindowTitle),
                new PropertyMetadata(new SolidColorBrush((Color)ColorConverter.ConvertFromString("#f0f0f0")), new PropertyChangedCallback(OnPorpertyChanged)));

        /// <summary>
        /// 标题字体颜色
        /// </summary>
        public Brush TitleForeground
        {
            get { return (Brush)this.GetValue(TitleForegroundProperty); }
            set { this.SetValue(TitleForegroundProperty, value); }
        }
        public static readonly DependencyProperty TitleForegroundProperty =
            DependencyProperty.Register("TitleForeground", typeof(Brush), typeof(WindowTitle),
                new PropertyMetadata(new SolidColorBrush((Color)ColorConverter.ConvertFromString("#333")), new PropertyChangedCallback(OnPorpertyChanged)));

        public Window WindowName
        {
            get { return (Window)this.GetValue(WindowNameProperty); }
            set { this.SetValue(WindowNameProperty, value); }
        }
        public static readonly DependencyProperty WindowNameProperty =
            DependencyProperty.Register("WindowName", typeof(Window), typeof(WindowTitle),
                new PropertyMetadata(default(Window), new PropertyChangedCallback(OnPorpertyChanged)));

        public Visibility IsShowProtocol
        {
            get { return (Visibility)this.GetValue(IsShowProtocolProperty); }
            set { this.SetValue(IsShowProtocolProperty, value); }
        }
        public static readonly DependencyProperty IsShowProtocolProperty =
            DependencyProperty.Register("IsShowProtocol", typeof(Visibility), typeof(WindowTitle),
                new PropertyMetadata(Visibility.Visible, new PropertyChangedCallback(OnPorpertyChanged)));
        /// <summary>
        /// 关闭按钮命令
        /// </summary>
        public RelayCommand<Window> CloseCommand
        {
            get => (RelayCommand<Window>)this.GetValue(CloseCommandProperty);
            set => SetValue(CloseCommandProperty, value);
        }
        public static readonly DependencyProperty CloseCommandProperty = DependencyProperty.Register("CloseCommand", typeof(RelayCommand<Window>), typeof(WindowTitle));

        private static void OnPorpertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            (d as WindowTitle).Init_Title();
        }
        private void Init_Title()
        {
            if (TitleBackground != null)
                TitleBorder.Background = TitleBackground;

            if (TitleForeground != null)
                txtTitle.Foreground = BtnClose.Foreground = BtnMin.Foreground = btnProtocol.Foreground = TitleForeground;

            if (WindowName != null)
                txtTitle.Text = WindowName.Title;

            ProtocolWindow.Visibility = IsShowProtocol;
        }

        #endregion
        public WindowTitle()
        {
            InitializeComponent();
            
        }

        private void CloseWindow_Click(object sender, RoutedEventArgs e)
        {
            if (WindowName != default(Window) && CloseCommand != null)
            {
                CloseCommand.Execute(WindowName);
            }
        }

        private void MinWindow_Click(object sender, RoutedEventArgs e)
        {
            if (WindowName != default(Window))
            {
                WindowName.WindowState = WindowState.Minimized;
            }
        }

        private void ProtocolWindow_Click(object sender, RoutedEventArgs e)
        {
            ProtocolView protocolView = new();
            protocolView.ShowDialog();
        }

        /// <summary>
        /// 窗口移动事件
        /// </summary>
        private void TitleBar_MouseMove(object sender, MouseEventArgs e)
        {
            if (WindowName != default(Window))
            {
                if (e.LeftButton == MouseButtonState.Pressed)
                {
                    WindowName.DragMove();
                }
            }
        }
    }
}
